using System;
using System.Collections;
using System.Web;
using System.Web.UI.WebControls;
using NCS.Library;
using NCS.Library.Components;
using NCS.Library.Components.Portal;
using NCS.Library.Components.Security;
using NCS.Library.Utilities;
using NCS.Module.CMS.Library.Zones;
using PortalController=NCS.Library.Components.Portal.PortalController;

namespace Portal.Administrator.Portals
{
	public partial class PortalCacheManager : AuthenticatedControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
            lblUpdateStatus.Text = string.Empty;

            if (!IsPostBack)
            {
                //Fill portals
                if (!CurrentUser.IsSuperAdmin && CurrentUser.IsAdmin)
                {
                    dropPortal.Items.Clear();
                    if (!CurrentUser.IsSuperAdmin && !CurrentUser.IsAdmin)
                    {
                        PortalController.LoadPortalsForUserID(dropPortal.Items, CurrentUser.UserID);
                    }
                    else
                    {
                        PortalController.LoadPortals(dropPortal.Items);
                    }
                    dropPortal.SelectedValue = AppEnv.PortalId();
                    LoadZones();
                    
                    pnlCacheAll.Visible = false;
                    pnlCacheByPortal.Visible = true;
                    btnRemoveAll.Visible = false;
                    btnRemoveCache.Visible = true;
                }
                else
                {
                    pnlCacheAll.Visible = true;
                    pnlCacheByPortal.Visible = false;
                    btnRemoveAll.Visible = true;
                    btnRemoveCache.Visible = false;
                    
                    HttpContext context = HttpContext.Current;
                    IEnumerator dcEnum = context.Cache.GetEnumerator();
                    ArrayList _cacheArray = new ArrayList();
                    while (dcEnum.MoveNext())
                        if (((DictionaryEntry)dcEnum.Current).Value != null)
                            _cacheArray.Add(((DictionaryEntry)dcEnum.Current).Key.ToString());
                    _cacheArray.Sort();
                    lstAllCache.DataSource = _cacheArray;
                    lstAllCache.DataBind();
                }
            }
		}

        protected void LoadZones()
        {
            dropZone.Items.Clear();            
            ZoneController.LoadZones(dropZone.Items, dropPortal.SelectedValue);
            dropZone.Items.Insert(0, new ListItem("Root", "0"));
            dropZone_SelectedIndexChanged(null,null);
       }

        protected void dropPortal_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadZones();
        }

		protected void btnRemoveCache_Click(object sender, EventArgs e)
		{
			DataCaching data = new DataCaching();
            foreach (ListItem item in lstCache.Items)
            {
                data.RemoveCache(item.Text);
            }
            lstCache.DataBind();
            lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
            this.AddLog("Portal Cache", " try remove cache success");
		}

        protected void btnRemoveAll_Click(object sender, EventArgs e)
        {
            DataCaching data = new DataCaching();
            foreach (ListItem item in lstAllCache.Items)
                data.RemoveCache(item.Text);
            lstAllCache.DataBind();
            lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
            this.AddLog("Portal Cache", " try remove all cache success");
        }
		
        protected void dropZone_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstCache.DataSource = PortalCacheController.GetPortalCacheByPortalIdAndZoneID(ConvertUtility.ToInt32(dropPortal.SelectedValue), ConvertUtility.ToInt32(dropZone.SelectedValue));
            lstCache.DataTextField = "CacheKey";
            lstCache.DataValueField = "Portal_Cache_ID";
            lstCache.DataBind();
        }
	}
}